/**
 * 使用 使用原生node 浏览小说网站
 */
const http = require('https');
const cheerio = require("cheerio")
const download = require("download")
const host = "https://m.banzhuchilaohu.com"
http.get('https://m.banzhuchilaohu.com/indexlist/2916/', (resp) => {
  let data = [];

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    //   data += chunk;
    data.push(chunk)
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(data);
    let str = ""
    str = Buffer.concat(data).toString("utf-8");
    //   console.log(str)
    let $ = cheerio.load(str); //  cheerio页面内容  jquery读取标签
    let files = [];
    $(".chapter li > a").each((index, item) => { //循环获取内容
      console.log(host + $(item).attr("href"))
      files.push(host + $(item).attr("href"))
    })
    console.log(files) //打印 存入的内容
    let imgArr = [];
    imgArr.push("https://www.baidu.com/img/flexible/logo/pc/result.png");
    Promise.all(imgArr.map(x => download(x, "dist"))).then(() => { //使用download模块下载文件
      console.log("files download!")
    })
  });
  //   baidu图片地址  https://www.baidu.com/img/flexible/logo/pc/result.png
}).on("error", (err) => {
  console.log("Error: " + err.message);
});